package com.neuq.animaladoption.controller;

import com.neuq.animaladoption.pojo.Animal;
import com.neuq.animaladoption.pojo.PageBean;
import com.neuq.animaladoption.pojo.Result;
import com.neuq.animaladoption.service.AnimalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/animal")
public class AnimalController {

    @Autowired
    private AnimalService animalService;

    @PostMapping
    public Result add(@RequestBody @Validated(Animal.Add.class) Animal animal){
        animalService.add(animal);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Animal>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String animalType,
            @RequestParam(required = false) String animalState,
            @RequestParam(required = false) Integer shelterId
    ){
        PageBean<Animal> pb =  animalService.list(pageNum,pageSize,animalType,animalState,shelterId);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result<Animal> detail(Integer id){
        Animal animal = animalService.findById(id);
        return Result.success(animal);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Animal.Update.class) Animal animal){
        animalService.update(animal);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        animalService.deleteById(id);
        return Result.success();
    }

}
